#include "Enum.h"


CEnum::CEnum()
{
	m_nCurSel = 0;
}


CEnum::~CEnum()
{
}

bool CEnum::appItem(char *pszName, int value)
{
	auto itor = m_mapItems.find(pszName);
	if (itor != m_mapItems.end()) {
		return false;
	}

	for (auto item : m_mapItems) {
		if (item.second == value) {
			return false;
		}
	}

	m_mapItems[pszName] = value;
	
	if (m_mapItems.size() == 1) {
		m_nCurSel = value;
	}

	return true;
}

std::map<std::string, int> & CEnum::getItems()
{
	return m_mapItems;
}

bool CEnum::getItemName(IN const int value, OUT std::string &strName)
{
	for (auto item : m_mapItems) {
		if (item.second == value) {
			strName = item.first;
			return true;
		}
	}

	return false;
}

bool CEnum::getItemValue(IN char *pszName, OUT int & value)
{
	auto itor = m_mapItems.find(pszName);
	if (itor == m_mapItems.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

bool CEnum::getCurSel(OUT int &value)
{
	value = m_nCurSel;
	return true;
}

bool CEnum::getCurSel(OUT std::string &strName)
{
	return getItemName(m_nCurSel, strName);
}

bool CEnum::select(char *pszName)
{
	return getItemValue(pszName, m_nCurSel);
}

bool CEnum::select(int value)
{
	for (auto item : m_mapItems) {
		if (item.second == value) {
			m_nCurSel = value;
			return true;
		}
	}

	return false;
}
